package  com.diamond.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diamond.bo.UserFunctionBO;
import com.diamond.facade.UserFunctionFacade;
import com.diamond.util.JsonMSG;
import com.diamond.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * UserperAction ������
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class UserFunctionAction extends ActionSupport {

	private static Logger log=LoggerFactory.getLogger(UserFunctionAction.class);
	private static final long serialVersionUID = 1867918549122117750L;
	private HttpServletRequest request = ServletActionContext.getRequest();
//	private IUserperService userperService;
	private UserFunctionFacade userFunctionFacade;
	private UserFunctionBO userFunctionBo;
	private List dataList;
	private String tips;
	private String json;
	
	public String saveOrUpdateUserFunction() {
		String userId = ServletActionContext.getRequest().getParameter("userId");
		String userFunctionBoStr =  ServletActionContext.getRequest().getParameter("data");
//		userFunctionBo=(UserFunctionBO) JsonUtils.toObject(json, UserFunctionBO.class);
		List<UserFunctionBO> userFunctionBolist = new ArrayList<UserFunctionBO>();
		if (!StringUtil.isEmpty(userFunctionBoStr)) {
			userFunctionBolist= (List<UserFunctionBO>) JSONArray.toCollection(JSONArray.fromObject(userFunctionBoStr), UserFunctionBO.class);
		}
		try {
			Boolean result = userFunctionFacade.saveOrUpdateUserFunction(userId, userFunctionBolist);
			if (result)
				this.json=JsonMSG.sucess("操作成功！").toJson();
			else
				this.json=JsonMSG.failed("提示，操作失败").toJson();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("用户权限分配错误",e);
			this.json=JsonMSG.failed("提示，操作失败"+e).toJson();
		}
		return "json";
	}

	public String queryUserFunction() {
		try {
			String userId = ServletActionContext.getRequest().getParameter("userId");
			List<UserFunctionBO> list = userFunctionFacade.queryUserFunction(userId);
			this.json=JsonMSG.sucess(list).toJson();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询失败",e);
			this.json=JsonMSG.failed("查询失败！").toJson();
		}
		return "json";
	}
   	
	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public UserFunctionFacade getUserFunctionFacade() {
		return userFunctionFacade;
	}
	
	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}
 
	public UserFunctionBO getUserFunctionBo() {
		return userFunctionBo;
	}

	public void setUserFunctionBo(UserFunctionBO userFunctionBo) {
		this.userFunctionBo = userFunctionBo;
	}

	public void setUserFunctionFacade(UserFunctionFacade userperFacade) {
		this.userFunctionFacade = userperFacade;
	}

	public UserFunctionBO getUserFunction() {
		return userFunctionBo;
	}

	public void setUserFunction(UserFunctionBO userFunctionBo) {
		this.userFunctionBo = userFunctionBo;
	}
    
	public String getTips() {
		return tips;
	}

	public void setTips(String tips) {
		this.tips = tips;
	}
}
